package com.endbd.marketing.control;


import com.endbd.feignapi.model.marketing.Custom;
import com.endbd.feignapi.model.marketing.Order;
import com.endbd.feignapi.model.marketing.Orderdetails;
import com.endbd.feignapi.model.purchase.CBrand;
import com.endbd.feignapi.model.purchase.CProduct;
import com.endbd.feignapi.model.purchase.CType;
import com.endbd.feignapi.model.system.City;
import com.endbd.feignapi.model.system.Job;
import com.endbd.feignapi.model.system.Province;
import com.endbd.feignapi.model.system.User;
import com.endbd.marketing.model.*;
import com.endbd.marketing.service.IMarketingService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
@RequestMapping("/marketing")
public class MarketingControl {

    @Autowired
    private IMarketingService marketingService;


    /**
     * 客户管理模块的添加功能实现
     * @param request
     * @return
     */
    @RequestMapping("/addMarketingCustom")
    @ResponseBody
    public ResultMessage addMarketingCustom(HttpServletRequest request,Custom custom){
        Map<String, String[]> parameterMap = request.getParameterMap();
        BeanUtils.copyProperties(custom,parameterMap);
        marketingService.addMarketingCustom(custom);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("在客户管理页面进行了添加客户"+custom.getCustomName()+"的操作");
        return rm;
    }


    /**
     * 客户管理模块的修改功能实现
     * @param request
     * @return
     */
    @RequestMapping("/updateMarketingCustom")
    @ResponseBody
    public ResultMessage updateMarketingCustom(HttpServletRequest request,Custom custom){
        Map<String, String[]> parameterMap = request.getParameterMap();
        BeanUtils.copyProperties(custom,parameterMap);
        marketingService.updateMarketingCustom(custom);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("在客户管理页面对客户"+custom.getCustomName()+"进行了修改操作");
        return rm;
    }


    /**
     * 客户管理模块的注销与恢复按钮的功能实现
     * @param conn
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResultMessage updateStatus(MarketingCondition conn ){
        ResultMessage rm=new ResultMessage();
        Custom custom1 = marketingService.queryMarketingViewByCondition(conn);
        if (conn.getStatus().equals("2")){
            conn.setStatus("1");
            rm.setMsg("在客户管理页面对客户"+custom1.getCustomName()+"进行了恢复操作");
        }else {
            conn.setStatus("2");
            rm.setMsg("在客户管理页面对客户"+custom1.getCustomName()+"进行了注销操作");
        }
        marketingService.updateStatus(conn);
        rm.setStatus("200");
        rm.setFlag(true);
        return rm;
    }
    
    /**
     * 客户管理模块的分配按钮的职位、姓名的下拉菜单功能实现
     * @param conn
     * @return
     */
    @RequestMapping("/queryName")
    @ResponseBody
    public List<User> queryName(MarketingCondition conn){
        conn.setService("市场组");
        List<User> users = marketingService.queryName(conn);
        return users;
    }

    /**
     * 客户管理模块的分配功能实现
     * @param serviceId
     */
    @RequestMapping("/assignment")
    @ResponseBody
    public ResultMessage assignment(Integer serviceId,Integer customId){
        MarketingCondition conn=new MarketingCondition();
        conn.setServiceId(serviceId);
        conn.setCustomId(customId);
        conn.setDistracTime(new Date());
        marketingService.assignment(conn);
        Custom custom = marketingService.queryMarketingViewByCondition(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("在客户管理页面对客户"+custom.getCustomName()+"进行了分配操作");
        return rm;
    }

    /**
     * 客户浏览及客户管理页面的分页查询功能实现
     * @param pageBean
     * @param conn
     * @return
     */
    @RequestMapping("/marketingPage")
    @ResponseBody
    public ResultMessage queryMarketingPageByCondition( PageBean<Custom> pageBean, MarketingCondition conn ){
        if (conn.getProvince().equals("0")){
            conn.setProvince(null);
        }
        if (conn.getCity().equals("0")||conn.getCity().equals("请选择城市")){
            conn.setCity(null);
        }
        if ("0".equals(conn.getStatus())||"请选择".equals(conn.getStatus())){
            conn.setStatus(null);
        }
        if ("2".equals(conn.getService())){
            conn.setService("");
        }
        if ("1".equals(conn.getService())){
            conn.setService("1");
        }
        //转义 %
        String company="";
        if(conn.getCompany().contains("%")||conn.getCompany().contains("_")){
            company= conn.getCompany().replaceAll("%","/%");
            company = company.replaceAll("_","/_");
            conn.setCompany(company);
        }
        //转义 下划线
        String customname="";
        if(conn.getCustomName().contains("_")||conn.getCustomName().contains("%")){
            customname = conn.getCustomName().replaceAll("_","/_");
            customname= customname.replaceAll("%","/%");
            conn.setCustomName(customname);
        }
        List<City> cities = marketingService.queryCity(conn);
        List<Integer> i = new ArrayList<>();
        for (City city : cities) {
            i.add(city.getId());
        }
            conn.setAddress(i);
        PageBean<Custom>  pb = marketingService.queryMarketingPageByCondition(pageBean,conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(pb);

        return rm;
    }

    /**
     * 客户管理及客户详情模块的查看详情功能实现
     * @param customId
     * @param province
     * @param city
     * @return
     */
    @RequestMapping(value = "/marketingView/{customId}/{province}/{city}")
    @ResponseBody
    public ResultMessage queryMarketingViewByCondition(@PathVariable(value = "customId") String customId,@PathVariable(value = "province") String province,@PathVariable(value = "city") String city){
        MarketingCondition conn=new MarketingCondition();
        conn.setCustomId(Integer.valueOf(customId));
        conn.setCity(city);
        City city2 = marketingService.queryCityByMarketingCondition(conn);
        City city1=new City();
        city1.setId(city2.getId());
        Province province1=new Province();
        province1.setId(city2.getPId());
        Custom pb = marketingService.queryMarketingViewByCondition(conn);
        Integer createId = pb.getCreateId();
        User user = marketingService.queryUserBycreateId(createId);
        pb.setCreator(user);
        pb.setCity(city1);
        pb.setProvince(province1);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(pb);
        return rm;
    }

    /**
     * 客户管理及客户详情模块的查询条件省、市的下拉菜单功能实现
     * @param conn
     * @return
     */
    @RequestMapping("/queryCity")
    @ResponseBody
    public List<City> queryCity(MarketingCondition conn){
        List<City> cities = marketingService.queryCity(conn);
        return cities;
    }

    /**
     * 订单管理及订单审核页面的分页查询功能实现
     * @param pageBean
     * @param conn
     * @return
     */
    @RequestMapping("/marketingOrdersPage")
    @ResponseBody
    public ResultMessage queryMarketingOrdersPageByCondition(PageBean<Order> pageBean, MarketingOrdersCondition conn ){
        if ("99".equals(conn.getStatus())||"请选择".equals(conn.getStatus())){
                conn.setStatus("");
        }
        Double d;
        if (conn.getMinMoney()!=null&&conn.getMaxMoney()!=null) {
            if (conn.getMinMoney() > conn.getMaxMoney()) {
                d = conn.getMinMoney();
                conn.setMinMoney(conn.getMaxMoney());
                conn.setMaxMoney(d);
            }
        }
        Date date;
        if (conn.getStartTime()!=null&&conn.getEndTime()!=null) {
            if (conn.getStartTime().compareTo(conn.getEndTime()) > 0) {
                date = conn.getStartTime();
                conn.setStartTime(conn.getEndTime());
                conn.setEndTime(date);
            }
        }
        //转义 %
        String orderId="";
        if (conn.getOrderId()!=null) {
            if (conn.getOrderId().contains("%") || conn.getOrderId().contains("_")) {
                orderId = conn.getOrderId().replaceAll("%", "/%");
                orderId = orderId.replaceAll("_", "/_");
                conn.setOrderId(orderId);
            }
        }
        //转义 下划线
        String customname="";
        if (conn.getCustomName()!=null) {
            if (conn.getCustomName().contains("_") || conn.getCustomName().contains("%")) {
                customname = conn.getCustomName().replaceAll("_", "/_");
                customname = customname .replaceAll("%", "/%");
                conn.setCustomName(customname);
            }
        }
        PageBean<Order> pb = marketingService.queryMarketingOrdersListByCondition(pageBean, conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(pb);

        return rm;
    }

    /**
     * 订单添加页面查询所有的品牌
     * @return
     */
    @RequestMapping("/queryBrand")
    @ResponseBody
    public ResultMessage queryBrand(){
        List<CBrand> cBrands = marketingService.queryBrand();
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(cBrands);
        return rm;
    }


    /**
     * 订单添加页面查询品牌所对应的类型
     * @return
     */
    @RequestMapping("/queryType")
    @ResponseBody
    public ResultMessage queryTypeByBrandId(Integer brandId){
        List<CType> cTypes = marketingService.queryTypeByBrandId(brandId);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(cTypes);
        return rm;
    }

    /**
     * 订单添加页面查询类型所对应的型号
     * @param typeId
     * @return
     */
    @RequestMapping("/queryProduct")
    @ResponseBody
    public ResultMessage queryProductByTypeId(Integer typeId){
        List<CProduct> cProducts = marketingService.queryProductByTypeId(typeId);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(cProducts);
        return rm;
    }


    /**
     * 订单添加页面查询商品信息
     * @param productId
     * @return
     */
    @RequestMapping("/queryProductDetail")
    @ResponseBody
    public ResultMessage queryProductByproductId(Integer productId){
        List<CProduct> cProducts = marketingService.queryProductByproductId(productId);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(cProducts);
        return rm;
    }

    /**
     * 订单管理页面提交审核功能的查询职位功能
     * @param deptId
     * @return
     */
    @RequestMapping("/queryJob")
    @ResponseBody
    public ResultMessage queryJobByDeptId(Integer deptId){
        List<Job> jobs = marketingService.queryJobByDeptId(deptId);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(jobs);
        return rm;
    }

    /**
     * 订单管理页面提交审核功能的查询职位对应的姓名功能
     * @param jobId
     * @return
     */
    @RequestMapping("/queryUser")
    @ResponseBody
    public ResultMessage queryUserByJobId(Integer jobId){
        List<User> users = marketingService.queryUserByJobId(jobId);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(users);
        return rm;
    }

    /**
     * 订单管理页面添加功能内客户查询功能
     * @return
     */
    @RequestMapping("/queryCustom")
    @ResponseBody
    public ResultMessage queryCustom(){
        MarketingCondition conn=new MarketingCondition();
        List<Custom> custom = marketingService.queryMarketingListByCondition(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(custom);
        return rm;
    }


    /**
     * 订单管理页面添加功能内的订单添加功能
     * @param conn
     * @return
     */
    @RequestMapping("/addOrder")
    @ResponseBody
    public ResultMessage addOrder(MarketingOrdersCondition conn){
        conn.setStatus("0");
        marketingService.addOrder(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("在订购单管理页面对客户"+conn.getCustomName()+"进行了添加订单"+conn.getOrderId()+"的操作");
        return rm;
    }

    /**
     * 订单管理页面添加功能内的订单详情添加功能
     * @param request
     * @param conn
     * @return
     */
    @RequestMapping("/addOrderDetail")
    @ResponseBody
    public ResultMessage addOrderDetail(HttpServletRequest request, MarketingOrdersDetailsCondition conn){
        Map<String, String[]> parameterMap = request.getParameterMap();
        BeanUtils.copyProperties(conn,parameterMap);
        Orderdetails orderdetails = new Orderdetails();
        int i=0;
        while (i<conn.getProductId().length) {
            orderdetails.setProductId(conn.getProductId()[i]);
            orderdetails.setProPrice(conn.getProPrice()[i]);
            orderdetails.setPurchaseNum(conn.getPurchaseNum()[i]);
            orderdetails.setProTotal(conn.getProTotal()[i]);
            orderdetails.setOrderId(conn.getOrderId());
            marketingService.addOrderDetail(orderdetails);
            i++;
        }
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        return rm;
    }

    /**
     *订单管理页面查看详情功能实现
     * @param conn
     * @return
     */
    @RequestMapping("/queryOrderView")
    @ResponseBody
    public ResultMessage queryOrderViewByOrderId(MarketingOrdersCondition conn){
        List<Order> orders = marketingService.queryMarketingOrdersListByCondition(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(orders);
        return rm;
    }

    /**
     * 订单查看详情页面查询相关订单详情功能
     * @param conn
     * @return
     */
    @RequestMapping("/queryOrderDetails")
    @ResponseBody
    public ResultMessage queryOrderDetailsByOrderId(MarketingOrdersDetailsCondition conn){
        List<Orderdetails> orderdetails = marketingService.queryOrderDetailsByOrderId(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(orderdetails);
        return rm;
    }

    /**
     * 订单管理页面删除功能实现
     * @param orderId
     * @return
     */
    @RequestMapping("/delOrder")
    @ResponseBody
    public ResultMessage delOrder(String orderId){
        marketingService.delOrderDetail(orderId);
        marketingService.delOrder(orderId);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("对订单"+orderId+"进行了删除操作");
        return rm;
    }

    /**
     * 订单管理页面提交审核功能实现
     * @param order
     * @return
     */
    @RequestMapping("/orderExamine")
    @ResponseBody
    public ResultMessage orderExamine(Order order){
        order.setCheckTime(new Date());
        order.setStatus("1");
        marketingService.orderExamine(order);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("对订单"+order.getOrderId()+"进行了提交审核操作");
        return rm;
    }

    /**
     * 订单审核页面审核功能实现
     * @param order
     * @return
     */
    @RequestMapping("/orderAudit")
    @ResponseBody
    public ResultMessage orderAudit(Order order){
        marketingService.orderAudit(order);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("对订单"+order.getOrderId()+"进行了审核操作");
        return rm;
    }

    /**
     * 根据用户id查找用户地址功能
     * @param custom
     * @return
     */
    @RequestMapping("/queryAddress")
    @ResponseBody
    public ResultMessage queryAddressByCustomId(Custom custom){
        Custom custom1 = marketingService.queryAddressByCustomId(custom);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(custom1);
        return rm;
    }

    /**
     * 订单管理页面修改功能实现
     * @param conn
     * @return
     */
    @RequestMapping("/updateOrder")
    @ResponseBody
    public ResultMessage updateOrder(MarketingOrdersCondition conn){
        marketingService.delOrderDetail(conn.getOrderId());
        marketingService.updateOrder(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("对订单"+conn.getOrderId()+"进行了修改操作");
        return rm;
    }

    /**
     * 出库管理页面添加出库查询订单功能实现
     * @return
     */
    @RequestMapping("/queryOrderForWarehouse")
    @ResponseBody
    public ResultMessage queryOrderForWarehouse(){
        String status="2";
        Integer state=0;
        MarketingOrdersCondition conn=new MarketingOrdersCondition();
        conn.setStatus(status);
        conn.setState(state);
        List<Order> orders = marketingService.queryMarketingOrdersListByCondition(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        rm.setResultData(orders);
        return rm;
    }

    /**
     *  出库管理页面添加出库功能实现
     * @return
     */
    @RequestMapping("/updateOrderForWarehouse")
    @ResponseBody
    public ResultMessage updateOrderForWarehouse(HttpServletRequest request,MarketingOrdersCondition conn){
        Map<String, String[]> parameterMap = request.getParameterMap();
        BeanUtils.copyProperties(conn,parameterMap);
        Integer state=1;
        conn.setState(state);
//        marketingService.addOrderWarehouse(conn);
        marketingService.updateOrderWarehouse(conn);
        ResultMessage rm=new ResultMessage();
        rm.setStatus("200");
        rm.setFlag(true);
        rm.setMsg("");
        return rm;
    }
}
